package org.test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @author 飒飒
 * @ClassName AIQuestionAnswering
 * @description: TODO
 * @date 2024年04月01日
 * @version: 1.0
 */
public class AIQuestionAnswering {
    public static void main(String[] args){
        try {
            // 替换为你的AI问答接口URL
            String apiUrl = "http://82.157.234.84:8091/chat/new_chat?agent_chat_id=1&user_msg=%E4%BD%A0%E5%A5%BD%E5%91%80%2C%E5%B0%8F%E7%8B%97&user_id=8&model_name=ERNIE-Bot";
            URL url = new URL(apiUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");

            InputStream inputStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            // 逐行读取输出流
            while ((line = reader.readLine()) != null) {
                System.out.println(line); // 或者你可以做其他处理
                //将读取到的信息发响应到前端
            }

            // 关闭资源
            reader.close();
            inputStream.close();
            connection.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  /*  public static void main(String[] args) throws IOException {
        // 这里假设您已经有一个流数据的输入流
        InputStream inputStream = getYourInputStream();
        // 设置
        String apiUrl = "http://192.168.1.233:5000/chat/new_chat?agent_chat_id=1&user_msg=%E4%BD%A0%E5%A5%BD%E5%91%80%2C%E5%B0%8F%E7%8B%97&user_id=8&model_name=ERNIE-Bot";

        URL url = new URL(apiUrl);
        // 替换成您的API端点URL
        // 打开连接
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 设置POST请求
        connection.setRequestMethod("POST");
        // 允许输出
        connection.setDoOutput(true);
        // 获取连接的输出流
        OutputStream outputStream = connection.getOutputStream();
        // 将流数据写入输出流
        byte[] buffer = new byte[4096];
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
        // 关闭输入流和输出流
        inputStream.close();
        outputStream.close();
        // 获取响应码
         int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);
        // 读取响应内容（如果需要的话）
        InputStream responseStream = connection.getInputStream();
        // 这里可以对响应内容进行读取和处理
        // 关闭响应流
        responseStream.close();
        // 关闭连接
        connection.disconnect(); }
        // 模拟获取流数据的输入流
        private static InputStream getYourInputStream() {
            // 这里返回您的流数据的输入流
            // 示例中只是简单返回一个示例输入流
            return AIQuestionAnswering.class.getResourceAsStream("example.txt");
        }*/
}
